﻿namespace 下载网页
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            await DownloadWebPage("http://www.baidu.com", @"d:\1.txt");
            Console.WriteLine("下载完成");
            Console.ReadKey();
        }

        static async Task DownloadWebPage(string url, string filePath)
        {
            using (HttpClient httpclient=new HttpClient())
            {
                for (int i = 0; i < 3; i++)
                {
                    try
                    {
                        string s = await httpclient.GetStringAsync(url);
                        await File.WriteAllTextAsync(filePath, s);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"下载失败，正在重试...{i + 1}/3");
                        await Task.Delay(1000);
                        continue;
                    }

                    break;
                }
            }
        }
    }
}
